<!DOCTYPE html>
<html>
<head>
  <title>大丰收工具</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
  <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
  <script src="./js/scatterjs-core.min.js"></script>
  <script src="./js/scatterjs-plugin-eosjs.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/eosjs@16.0.9/lib/eos.min.js"></script>
  
</head>
<body>

<div class="container">

<div class="row">
  <div class="col"><h2>大丰收工具</h2></div>
  <!--div class="col"><button type="button" class="btn" id="login" onclick="login()">登陆</button></div-->
</div>
  
  <div class="card">
    <div class="card-header">USDX矿池</div>
    <div class="card-body">
	  <button type="button" class="btn btn-primary" id="claimUsdxUsdc" onclick="claimUsdxUsdc()">USDC矿池收矿</button>
	  <button type="button" class="btn btn-primary" id="claimUsdxDfs" onclick="claimUsdxDfs()">DFS矿池收矿</button>
	  <button type="button" class="btn btn-primary" id="claimUsdxAll" onclick="claimUsdxAll()">全部收矿</button>	
	</div> 
  </div>
  <br>
  
  <div class="card">
    <div class="card-header">DFS矿池</div>
    <div class="card-body">
	  <button type="button" class="btn btn-primary" id="claimDfs" onclick="claimDfs()">DFS矿池收矿</button>
	  <button type="button" class="btn btn-primary" id="claimDfsDss" onclick="claimDfsDss()">DFS金库收矿</button>
	  <button type="button" class="btn btn-primary" id="claimDfsAll" onclick="claimDfsAll()">全部收矿</button>	
	</div> 
  </div>
  <br>
  
  <div class="card">
    <div class="card-header">TAG矿池</div>
    <div class="card-body">
	  <button type="button" class="btn btn-primary" id="claimTag" onclick="claimTag()">TAG矿池收矿</button>
	  <button type="button" class="btn btn-primary" id="claimTagDss" onclick="claimTagDss()">TAG金库收矿</button>
	  <button type="button" class="btn btn-primary" id="claimTagAll" onclick="claimTagAll()">全部收矿</button>	
	</div> 
  </div>
  <br>
  
  <div class="card">
    <div class="card-header">YFC矿池</div>
    <div class="card-body">
	  <button type="button" class="btn btn-primary" id="claimYfcDss" onclick="claimYfcDss()">YFC金库收矿</button>
	  <button type="button" class="btn btn-primary" id="claimYfcAll" onclick="claimYfcAll()">全部收矿</button>	
	</div> 
  </div>
  <br>

  <div class="card">
    <div class="card-header">PDD农场</div>
    <div class="card-body">
	  <button type="button" class="btn btn-primary" id="claimPdd" onclick="claimPdd()">PDD农场收矿</button>
	</div> 
  </div>
  <br>

  <div class="card">
    <div class="card-header">自定义</div>
    <div class="card-body">
	  <button type="button" class="btn btn-primary">自定义1</button>
	  <button type="button" class="btn btn-primary">自定义2</button>	
	</div> 
  </div>
  <br>
  <div class="card">
    <div class="card-header">输出</div>
    <div class="card-body">
	  <textarea rows="20" cols="80" id="log"></textarea>	
	</div> 
  </div>

</div>

</body>

<script>
    ScatterJS.plugins( new ScatterEOS() );
	const network = ScatterJS.Network.fromJson({
		blockchain:'eos',
		chainId:'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
		host:'api-mainnet.starteos.io',
		port:443,
		protocol:'https'
	});
	// const network = ScatterJS.Network.fromJson({
	// 	blockchain:'eos',
	// 	chainId:'2a02a0053e5a8cf73a56ba0fda11e4d92e0238a4a2aa74fccf46d5a910746840',
	// 	host:'jungle3.cryptolions.io',
	// 	port:443,
	// 	protocol:'https'
	// });
	// console.log = function(msg) {
	// 	let str = $("#log").text() + "\n" + msg;
	// 	$("#log").text(str);
	// }
	let userLiqsMids = [];	
	let usdxLiqsMids = {usdc: [], dfs: [], usdd: []};
	let usdxPoolMids = {usdc: [], dfs: [], usdb: []};
	let tagLiqsMids = [];
	let dfsPoolMids = [];
	let tagPoolMids = [];
	let account;
	let eos;

	async function login() {
		if (await ScatterJS.connect('MyAppName', {network})) {
			console.log("连接成功");
			await ScatterJS.logout()
			let ret = await ScatterJS.login().catch(err => {
				if (err.isError) {
					console.log(err);
					console.log("登陆失败")
					return false;
				}
			});
			if (ret !== false) {
				console.log("登陆成功");
				account = ScatterJS.account('eos');
				eos = ScatterJS.eos(network, Eos);
				console.log(account);
				liqs = await getUserLiqs();
			}
		} else {
			console.log("连接失败");
			return false;
		}
	}

	async function getUserLiqs() {
		usdxPoolMids.usdc = await eos.getTableRows({"code":"usdxvotevote","scope":"usdxvotevote","table":"pools","json":true,"limit":100}).then(data => {
			data.rows.sort((a, b) => {
				return b.total_votes - a.total_votes;
			});

			let mids = [];
			for (let i=0; i<21; i++) {
				mids.push(data.rows[i].mid);
			}
			console.log(mids);
			return mids;
		});

		usdxPoolMids.dfs = await eos.getTableRows({"code":"usdxvotevote","scope":"usdxvotevote","table":"pools2","json":true,"limit":100}).then(data => {
			data.rows.sort((a, b) => {
				return b.total_votes - a.total_votes;
			});

			let mids = [];
			for (let i=0; i<21; i++) {
				mids.push(data.rows[i].mid);
			}
			console.log(mids);
			return mids;
		});

		dfsPoolMids = await eos.getTableRows({"code":"dfspoolsvote","scope":"dfspoolsvote","table":"pools","json":true,"limit":21, "index_position":2,"key_type":"float64"}).then(data => {
			let mids = [];
			for (let i=0; i<21; i++) {
				mids.push(data.rows[i].mid);
			}
			console.log(mids);
			return mids;
		});

		PddPoolMids = await eos.getTableRows({"code":"dfspoolsvote","scope":"dfspoolsvote","table":"pools","json":true,"limit":10, "index_position":2,"key_type":"float64"}).then(data => {
			let mids = [];
			for (let i=0; i<10; i++) {
				mids.push(data.rows[i].mid);
			}
			if (mids.includes(346) === false) {
				mids.push(346);
			}
			console.log(mids);
			return mids;
		});

		tagPoolMids = await eos.getTableRows({"code":"vote.tag","scope":"vote.tag","table":"pools","json":true,"limit":15, "index_position":2,"key_type":"float64"}).then(data => {
			let mids = [];
			for (let i=0; i<15; i++) {
				mids.push(data.rows[i].mid);
			}
			console.log(mids);
			return mids;
		});

		//user liqs mid
		await eos.getTableRows({"code": "defislogsone","scope": account.name,"table": "liqs2","json": true,"limit": 100}).then(data => {
			for (let i=0; i<data.rows.length; i++) {
				userLiqsMids.push(data.rows[i].mid); //需要放在最前面

				if ((data.rows[i].bal0.lastIndexOf("TAG") != -1) ||
					(data.rows[i].bal1.lastIndexOf("TAG") != -1)) {
					tagLiqsMids.push(data.rows[i].mid);
				}

				if ((data.rows[i].bal0.lastIndexOf("USDC") != -1) ||
					(data.rows[i].bal1.lastIndexOf("USDC")) != -1) {
					usdxLiqsMids.usdc.push(data.rows[i].mid);
					continue;
				}

				if ((data.rows[i].bal0.lastIndexOf("DFS") != -1) ||
					(data.rows[i].bal1.lastIndexOf("DFS") != -1)) {
					usdxLiqsMids.dfs.push(data.rows[i].mid);
				}
			}
		});
	}

	async function claimPdd() {
		let actions = [];
		console.log(userLiqsMids);
		userLiqsMids.forEach(mid => {
			if (PddPoolMids.includes(mid)) {
				actions.push({
					account: 'pddfarmers11',
					name: 'feed',
					authorization: [{
						actor: account.name,
						permission: 'active'
					}],	
					data: {
						id: mid,
						user: account.name
					}				
				});
			}
		});

		if (actions.length !== 0) {
			eos.transaction({
				actions: actions
			}).then(trx => {
				console.log('trx', trx);
			}).catch(err => {
				console.error(err);
			});	
		} else {
			console.log("你的做市交易对不在DFS矿池排名范围内");
		}
	}

	async function claimTag() {
		let actions = [];
		console.log(tagLiqsMids);
		tagLiqsMids.forEach(mid => {
			if (tagPoolMids.includes(mid)) {
				actions.push({
					account: 'tagtokenfarm',
					name: 'claim',
					authorization: [{
						actor: account.name,
						permission: 'active'
					}],	
					data: {
						mid: mid,
						user: account.name
					}				
				});
			}
		});

		//投票挖矿，暂不加入
		// actions.push({
		// 	account: 'tagtokenfarm',
		// 	name: 'harvest',
		// 	authorization: [{
		// 		actor: account.name,
		// 		permission: 'active'
		// 	}],	
		// 	data: {
		// 		farmer: account.name
		// 	}				
		// });

		if (actions.length !== 0) {
			eos.transaction({
				actions: actions
			}).then(trx => {
				console.log('trx', trx);
			}).catch(err => {
				console.error(err);
			});	
		} else {
			console.log("你的做市交易对不在TAG矿池排名范围内");
		}
	}

	async function claimTagDss() {
		let actions = [];
		actions.push({
			account: 'dss.tag',
			name: 'claim',
			authorization: [{
				actor: account.name,
				permission: 'active'
			}],	
			data: {
				user: account.name
			}				
		});

		eos.transaction({
			actions: actions
		}).then(trx => {
			console.log('trx', trx);
		}).catch(err => {
			console.error(err);
		});	
	}

	async function claimTagAll() {
		let actions = [];
		console.log(tagLiqsMids);
		tagLiqsMids.forEach(mid => {
			if (tagPoolMids.includes(mid)) {
				actions.push({
					account: 'tagtokenfarm',
					name: 'claim',
					authorization: [{
						actor: account.name,
						permission: 'active'
					}],	
					data: {
						mid: mid,
						user: account.name
					}				
				});
			}
		});

		actions.push({
			account: 'dss.tag',
			name: 'claim',
			authorization: [{
				actor: account.name,
				permission: 'active'
			}],	
			data: {
				user: account.name
			}				
		});

		//投票挖矿，暂不加入
		// actions.push({
		// 	account: 'tagtokenfarm',
		// 	name: 'harvest',
		// 	authorization: [{
		// 		actor: account.name,
		// 		permission: 'active'
		// 	}],	
		// 	data: {
		// 		farmer: account.name
		// 	}				
		// });

		if (actions.length !== 0) {
			eos.transaction({
				actions: actions
			}).then(trx => {
				console.log('trx', trx);
			}).catch(err => {
				console.error(err);
			});	
		} else {
			console.log("你的做市交易对不在TAG矿池排名范围内");
		}
	}

	async function claimDfs() {
		let actions = [];
		console.log(userLiqsMids);
		userLiqsMids.forEach(mid => {
			if (dfsPoolMids.includes(mid)) {
				actions.push({
					account: 'miningpool11',
					name: 'claim',
					authorization: [{
						actor: account.name,
						permission: 'active'
					}],	
					data: {
						mid: mid,
						user: account.name
					}				
				});
			}
		});

		if (actions.length !== 0) {
			eos.transaction({
				actions: actions
			}).then(trx => {
				console.log('trx', trx);
			}).catch(err => {
				console.error(err);
			});	
		} else {
			console.log("你的做市交易对不在DFS矿池排名范围内");
		}
	}

	async function claimDfsDss() {
		let actions = [];
		actions.push({
			account: 'dfsdsrsystem',
			name: 'claim',
			authorization: [{
				actor: account.name,
				permission: 'active'
			}],	
			data: {
				user: account.name
			}				
		});

		eos.transaction({
			actions: actions
		}).then(trx => {
			console.log('trx', trx);
		}).catch(err => {
			console.error(err);
		});	
	}

	async function claimDfsAll() {
		let actions = [];
		console.log(userLiqsMids);
		userLiqsMids.forEach(mid => {
			if (dfsPoolMids.includes(mid)) {
				actions.push({
					account: 'miningpool11',
					name: 'claim',
					authorization: [{
						actor: account.name,
						permission: 'active'
					}],	
					data: {
						mid: mid,
						user: account.name
					}				
				});
			}
		});

		actions.push({
			account: 'dfsdsrsystem',
			name: 'claim',
			authorization: [{
				actor: account.name,
				permission: 'active'
			}],	
			data: {
				user: account.name
			}				
		});

		eos.transaction({
			actions: actions
		}).then(trx => {
			console.log('trx', trx);
		}).catch(err => {
			console.error(err);
		});	
	}

	async function claimYfcDss() {
		let actions = [];
		actions.push({
			account: 'yfcdsssystem',
			name: 'claim',
			authorization: [{
				actor: account.name,
				permission: 'active'
			}],	
			data: {
				user: account.name
			}				
		});

		eos.transaction({
			actions: actions
		}).then(trx => {
			console.log('trx', trx);
		}).catch(err => {
			console.error(err);
		});	
	}

	async function claimYfcAll() {
		let actions = [];
		actions.push({
			account: 'yfcdsssystem',
			name: 'claim',
			authorization: [{
				actor: account.name,
				permission: 'active'
			}],	
			data: {
				user: account.name
			}				
		});

		eos.transaction({
			actions: actions
		}).then(trx => {
			console.log('trx', trx);
		}).catch(err => {
			console.error(err);
		});	
	}

	async function claimUsdxDfs() {
		let actions = [];
		usdxLiqsMids.dfs.forEach(mid => {
			if (usdxPoolMids.dfs.includes(mid)) {
				actions.push({
					account: 'usdxcorefarm',
					name: 'claim',
					authorization: [{
						actor: account.name,
						permission: 'active'
					}],	
					data: {
						mid: mid,
						user: account.name
					}				
				});
			}
		});

		if (actions.length !== 0) {
			eos.transaction({
				actions: actions
			}).then(trx => {
				console.log('trx', trx);
			}).catch(err => {
				console.error(err);
			});	
		} else {
			console.log("你的DFS做市交易对不在USDX排名范围内");
		}
	}

	async function claimUsdxUsdc() {
		let actions = [];
		usdxLiqsMids.usdc.forEach(mid => {
			if (usdxPoolMids.usdc.includes(mid)) {
				actions.push({
					account: 'usdxcorefarm',
					name: 'claim',
					authorization: [{
						actor: account.name,
						permission: 'active'
					}],	
					data: {
						mid: mid,
						user: account.name
					}				
				});
			}
		});

		if (actions.length !== 0) {
			eos.transaction({
				actions: actions
			}).then(trx => {
				console.log('trx', trx);
			}).catch(err => {
				console.error(err);
			});	
		} else {
			console.log("你的USDC做市交易对不在USDX排名范围内");
		}
	}

	async function claimUsdxAll() {
		let actions = [];
		usdxLiqsMids.usdc.forEach(mid => {
			if (usdxPoolMids.usdc.includes(mid)) {
				actions.push({
					account: 'usdxcorefarm',
					name: 'claim',
					authorization: [{
						actor: account.name,
						permission: 'active'
					}],	
					data: {
						mid: mid,
						user: account.name
					}				
				});
			}
		});
		usdxLiqsMids.dfs.forEach(mid => {
			if (usdxPoolMids.dfs.includes(mid)) {
				actions.push({
					account: 'usdxcorefarm',
					name: 'claim',
					authorization: [{
						actor: account.name,
						permission: 'active'
					}],	
					data: {
						mid: mid,
						user: account.name
					}				
				});
			}
		});

		if (actions.length !== 0) {
			eos.transaction({
				actions: actions
			}).then(trx => {
				console.log('trx', trx);
			}).catch(err => {
				console.error(err);
			});	
		} else {
			console.log("你的USDC与DFS做市交易对不在USDX排名范围内");
		}
	}

	login();
</script>

</html>